package com.woke.addressactivity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.http.HttpMethods;
import com.jakewharton.rxbinding2.view.RxView;
import com.woke.fragment.PromoteCodeFragment;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import java.util.concurrent.TimeUnit;

import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.OnekeyShareTheme;
import io.reactivex.functions.Consumer;

/**
 * 用戶-->推广码
 */
public class UserpromoteActivity extends BaseActivity implements
        OnClickListener, OnCheckedChangeListener, OnPageChangeListener {

    private RadioButton mLoanRb;
    private RadioButton mCreditRb;
    private RadioGroup mRadioGroup;
    private ViewPager mViewPager;
    private ChangePromoteAdapter mPromoteAdapter;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userpromote);
        findViewById(R.id.avmywalletnocard_image_back).setOnClickListener(this);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mRadioGroup = (RadioGroup) findViewById(R.id.changeRg);
        mLoanRb = (RadioButton) findViewById(R.id.loanRb);
        mCreditRb = (RadioButton) findViewById(R.id.creditRb);
        mRadioGroup.setOnCheckedChangeListener(this);

        mLoanRb.setChecked(true);


        mPromoteAdapter = new ChangePromoteAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mPromoteAdapter);
        mViewPager.setOnPageChangeListener(this);
        mViewPager.setCurrentItem(1);

        TextView mShareTv = (TextView) findViewById(R.id.share_tv);
        RxView.clicks(mShareTv).throttleFirst(2, TimeUnit.SECONDS).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                MyApp application = (MyApp) getApplication();
                if (application.getDatas_load() != null) {
                    String userid = application.getDatas_load().getId();
                    String type = mCreditRb.isChecked() ? "2" : "1";
                    showShare(HttpMethods.BASEURL+"g=Api&m=User&a=promocode_share&userId=" + userid + "&type=" + type);
                }
            }
        });
        myClipboard = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE);
    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        switch (view.getId()) {
            case R.id.avmywalletnocard_image_back:
                finish();
                break;

            //
            // case R.id.user_text1:
            // Log.e("onclick", "中财推广");
            // Intent intent = new Intent(UserpromoteActivity.this,
            // UserpromoteIIActivity.class);
            // intent.putExtra("title", "中财推广");
            // startActivity(intent);
            // Log.e("onclick", "中财推广");
            // break;
            //
            // case R.id.user_text2:
            // Intent intent2 = new Intent(UserpromoteActivity.this,
            // UserpromoteIIActivity.class);
            // intent2.putExtra("title", "平安i货");
            // startActivity(intent2);
            // break;
            // default:
            // break;
        }

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.loanRb:
                mViewPager.setCurrentItem(1, false);
                mCreditRb.setChecked(false);
                break;
            case R.id.creditRb:
                mLoanRb.setChecked(false);
                mViewPager.setCurrentItem(0, false);
                break;
            default:
                break;
        }

    }

	/*----------------------------------------------------------------------*/

    private class ChangePromoteAdapter extends FragmentPagerAdapter {

        public ChangePromoteAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }


        @Override
        public Fragment getItem(int arg0) {
            PromoteCodeFragment fragment = new PromoteCodeFragment();
            Bundle args = new Bundle();
            if (arg0 == 0) {
                args.putString("type", PromoteCodeFragment.TYPE_CREDIT);
            } else {
                args.putString("type", PromoteCodeFragment.TYPE_LOAN);
            }
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 2;
        }

    }


    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int arg0) {
        if (arg0 == 0) {
            mLoanRb.setChecked(false);
            mCreditRb.setChecked(true);
        } else {
            mLoanRb.setChecked(true);
            mCreditRb.setChecked(false);
        }

    }

    // 分享
    private ClipData myClip;
    private ClipboardManager myClipboard;

    private void showShare(final String url) {
//        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        // 参考代码配置章节，设置分享参数
        // 构造一个图标
        Bitmap enableLogo = BitmapFactory.decodeResource(getResources(), R.drawable.ssdk_oks_logo_fuzhi);
        String label = "复制链接";
        OnClickListener listener = new OnClickListener() {
            public void onClick(View v) {
                myClip = ClipData.newPlainText("text", url);
                myClipboard.setPrimaryClip(myClip);
                Toast.makeText(getApplicationContext(), "已复制到剪贴板",
                        Toast.LENGTH_SHORT).show();
            }
        };
        oks.setTheme(OnekeyShareTheme.CLASSIC);
//		 oks.show(HtmlActivity.this);
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
//		 oks.addHiddenPlatform(label);
        oks.setCustomerLogo(enableLogo, label, listener);
//		 oks.setViewToShare(enableLogo);
        // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(getResources().getString(R.string.oem_title_tuijian_share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl(url);
        // text是分享文本，所有平台都需要这个字段
        oks.setText(getResources().getString(R.string.oem_content_tuijian_share));
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImageUrl(getResources().getString(R.string.oem_url_tuijian_share));
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(url);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment(getResources().getString(R.string.app_name));
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getResources().getString(R.string.oem_title_tuijian_share));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(url);
        oks.setVenueName(getResources().getString(R.string.app_name));
        // 启动分享GUI
        oks.show(this);
    }

}
